package com.example.newinfinity.View.Layer

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import com.example.newinfinity.IconModel.IconModelType
import com.example.newinfinity.IconModel.IconState
import com.example.newinfinity.View.DrawShape.ArrowShape
import com.example.newinfinity.View.DrawShape.BezierShape
import com.example.newinfinity.View.DrawShape.CircleShape
import com.example.newinfinity.View.DrawShape.EraserShape
import com.example.newinfinity.View.DrawShape.FreeCurveShape
import com.example.newinfinity.View.DrawShape.LineShape
import com.example.newinfinity.View.DrawShape.LocationShape
import com.example.newinfinity.View.DrawShape.RectangleShape
import com.example.newinfinity.View.DrawShape.Shape
import com.example.newinfinity.View.DrawShape.TriangleShape

//表示一个图层 保存shapes并将其绘制到这个图层的bitmap上
class Layer(val id: Int, private val mWidth: Int, private val mHeight: Int) {
    private var mCanvas: Canvas
    private var mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888)

    //保存拖拉点拽操作中 上一次选中的图形
    private var lastSelectedShape: Shape? = null

    //保存拖拉点拽操作中 当前的选中图形
    private var currentSelectedShape: Shape? = null

    init {
        mCanvas = Canvas(mBitmap)   //使用该画笔绘制的图形均会保存到mBitmap上
    }

    //保存当前图层中的所有shapes
    private var mShapes: ArrayList<Shape> = arrayListOf()

    //提供给外部mBitmap画纸
    fun getBitmap(): Bitmap {
        return mBitmap
    }

    //绘制图形时 向mShapes中添加shape 并确认起始绘制点
    fun addShape(type: IconModelType, startX: Float, startY: Float) {
        when (type) {
            IconModelType.DRAW_CIRCLE -> {
                CircleShape().apply {
                    this.setStartPoint(startX, startY) //传入起始点坐标
                    mShapes.add(this)   //添加到mShapes中
                }
            }

            IconModelType.DRAW_RECTANGLE -> {
                RectangleShape().apply {
                    this.setStartPoint(startX, startY) //传入起始点坐标
                    mShapes.add(this)   //添加到mShapes中
                }
            }

            IconModelType.DRAW_LINE -> {
                LineShape().apply {
                    this.setStartPoint(startX, startY) //传入起始点坐标
                    mShapes.add(this)   //添加到mShapes中
                }
            }

            IconModelType.DRAW_CURVE -> {
                FreeCurveShape().apply {
                    this.setStartPoint(startX, startY) //传入起始点坐标
                    mShapes.add(this)   //添加到mShapes中
                }
            }

            IconModelType.DRAW_TRIANGLE -> {
                TriangleShape().apply {
                    this.setStartPoint(startX, startY) //传入起始点坐标
                    mShapes.add(this)   //添加到mShapes中
                }
            }

            IconModelType.DRAW_BEZIER -> {
                BezierShape().apply {
                    this.setStartPoint(startX, startY) //传入起始点坐标
                    mShapes.add(this)   //添加到mShapes中
                }
            }

            IconModelType.DRAW_LINE_ARROW -> {
                ArrowShape().apply {
                    this.setStartPoint(startX, startY) //传入起始点坐标
                    mShapes.add(this)   //添加到mShapes中
                }
            }

            IconModelType.DRAW_ERASER -> {
                EraserShape().apply {
                    this.setStartPoint(startX, startY) //传入起始点坐标
                    mShapes.add(this)   //添加到mShapes中
                }
            }

            IconModelType.DRAW_LOCATION -> {
                LocationShape().apply {
                    this.setStartPoint(startX, startY) //传入起始点坐标
                    mShapes.add(this)   //添加到mShapes中
                }
            }

            else -> {}
        }
    }

    //确认绘制图形时的终点位置
    fun addEndPoint(endX: Float, endY: Float) {
        getCurrentShape().setEndPoint(endX, endY)
    }

    //提供给外部统一绘制该图层中的所有shapes
    fun draw() {
        mBitmap.eraseColor(Color.TRANSPARENT)
        mShapes.forEach { shape ->
            shape.draw(mCanvas)
        }
    }

    //撤销最新的shape
    fun undo() {
        if (mShapes.isNotEmpty()) {
            mShapes.removeLast()
        }
    }

    //清空图层中的所有shapes
    fun clear() {
        if (mShapes.isNotEmpty()) {
            mShapes.clear()
        }
    }

    //获取mShapes中当前正在绘制的图形 为其确认终点位置
    private fun getCurrentShape(): Shape {
        return mShapes.last()
    }

    //专门提供给橡皮擦显示矩形的显示状态清空方法 DrawView中手up矩形显示及消失
    fun changeEraserRectShowState() {
        getCurrentShape().changeEraserRectShowState()
    }

    //用来判断当前点击点是否在图形的内部 进而选中是否填色
    fun fillColor(x: Float, y: Float) {
        if (mShapes.isEmpty()) return
        mShapes.asReversed().forEach { shape ->
            if (shape.containsPointInPath(x, y)) {
                shape.fillColor()
            }
        }
    }

    private fun returnCurrentSelectShape(x: Float, y: Float): Shape? {
        var selectedShape: Shape? = null
        mShapes.asReversed().forEach { shape ->
            if (shape.containsPointInRectF(x, y)) {
                selectedShape = shape
            }
        }
        return selectedShape
    }

    //用来判断点击点是否在图形移动区域内 进而拖拉点拽操作
    fun changeIconSelectedState(x: Float, y: Float) {
        if (mShapes.isEmpty()) return
        //找到当前的选中图形（可空）
        currentSelectedShape = returnCurrentSelectShape(x, y)
        if (currentSelectedShape != null) { //点击在某个图形的内部
            if (lastSelectedShape != null) {
                //此时有选中图形
                if (lastSelectedShape == currentSelectedShape) {
                    //二次选中 判断拖拉点拽的具体位置与操作
                    lastSelectedShape!!.calculateMoveType(x, y)
                } else {
                    //lastShape与currentShape不相同
                    //修改lastShape图形的选中状态
                    lastSelectedShape!!.changeIconSelectedState()
                    //保存当前选中图形 并修改其选中状态
                    lastSelectedShape = currentSelectedShape
                    lastSelectedShape!!.changeIconSelectedState()
                }
            } else {
                //此时没选中图形
                //保存这个首次选中的图形 并修改其选中状态
                lastSelectedShape = currentSelectedShape
                lastSelectedShape!!.changeIconSelectedState()
            }
        } else { //点击在图形外部
            // 判断此时是否有选中的图形->取消选中
            if (lastSelectedShape != null) {
                //修改lastShape图形的选中状态 取消选中
                lastSelectedShape!!.changeIconSelectedState()
                //清空当前的选中图形 -> 暂无选中图形
                lastSelectedShape = null
            }
        }
    }

    //点击每一个icon图标时 若界面中有移动move的选中图形需要将currentSelectedShape清空并修改其选中状态为MoveType.NONE
    fun clearMoveShape() {
        lastSelectedShape?.let {
            if (lastSelectedShape?.shapeSelectedState == IconState.SELECTED) {
                lastSelectedShape?.changeIconSelectedState()
                lastSelectedShape = null
            }
        }
    }

    //传入二次点击时 开始移动的实时坐标
    fun getMoveDistance(x: Float, y: Float) {
        //开始操作当前选中的图形，进行拖拉点拽操作操作
        lastSelectedShape?.getMoveDistance(x, y)
    }
}








